//
//  LZViewController.m
//  ThorWebViewSDK
//
//  Created by panfei mao on 01/15/2019.
//  Copyright (c) 2019 panfei mao. All rights reserved.
//

#import "LZViewController.h"
#import "LZLoginViewController.h"
#import "ThorFramework.h"

@interface LZViewController () <ThorWebViewDelegate,LoginViewDelegate>

@property (nonatomic, strong) UITextField   *urlTextField;
@property (nonatomic, strong) UIButton      *demoButton;
@property (nonatomic, strong) UIButton      *loginButton;
@property (nonatomic, strong) UIButton      *choiseUserButton;

@property (nonatomic, strong) UILabel       *userInfoLabel;

@end

@implementation LZViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.title = @"MarsWebDemo";
    self.view.backgroundColor = [UIColor whiteColor];
    
    CGFloat viewWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat textFieldX = 20.0;
    
    UITextField *userIdTextField = [[UITextField alloc] init];
    userIdTextField.placeholder = @"地址URL";
    userIdTextField.frame = CGRectMake(textFieldX, 100.0, viewWidth - 40.0, 40.0);
    userIdTextField.borderStyle = UITextBorderStyleLine;
    [self.view addSubview:userIdTextField];
    self.urlTextField = userIdTextField;
    self.urlTextField.text = @"https://th53.bailingpay.com/blhome/index?returnData=eyJqdW1wRnJvbSI6IjAyIiwiYXBwVHlwZSI6IkFQUCIsInRoaXJkQmFja1VybCI6Imh0dHBzOlwvXC9jZG4uNWljaG9uZy5jb21cL3N1Y2Nlc3MuaHRtbD9ibGluZyJ9&marsInfo=eyJtb2JpbGUiOiIxNTMwNTc5NzA5OCIsInByb2plY3RDb2RlIjoiU0hCVF9BaUNob25nX3lvbmdodSIsInByb2plY3RVc2VySWQiOjk2MDMsInNpZ24iOiIzYTJiY2ZkYmEzOGNiMWY1YTUyNThiMTBjODIzZGNjOCIsInByb2plY3RVc2VyVG9rZW4iOiIzN2NjYWRjOTA5ODY2NmFlMWY4OGZhZTJjN2I0MTQ0MiJ9&sdkInfo=ewogICJzZGtWZXJzaW9uIiA6ICIyMzEiCn0=";
    
    CGFloat demoButtonW = 160.0;
    CGFloat demoButtonH = 50.0;
    CGFloat loginButtonX = CGRectGetMinX(self.urlTextField.frame);
    CGFloat buttonMargin = (viewWidth - loginButtonX * 2 - demoButtonW * 2) / 2.0;
    CGFloat loginButtonY = CGRectGetMaxY(self.urlTextField.frame) + 50.0;
    
    
    self.loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.loginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.loginButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
    self.loginButton.layer.borderWidth = 1.0;
    self.loginButton.layer.borderColor = [UIColor blackColor].CGColor;
    self.loginButton.layer.cornerRadius = 4.0;
    self.loginButton.frame = CGRectMake(loginButtonX, loginButtonY, demoButtonW, demoButtonH);
    [self.view addSubview:self.loginButton];
    [self.loginButton addTarget:self action:@selector(loginButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.demoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.demoButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.demoButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.demoButton setTitle:@"Mars 测试入口" forState:UIControlStateNormal];
    self.demoButton.layer.borderWidth = 1.0;
    self.demoButton.layer.borderColor = [UIColor blackColor].CGColor;
    self.demoButton.layer.cornerRadius = 4.0;
    self.demoButton.frame = CGRectMake(CGRectGetMaxX(self.loginButton.frame) + buttonMargin, CGRectGetMinY(self.loginButton.frame), demoButtonW, demoButtonH);
    [self.view addSubview:self.demoButton];
    [self.demoButton addTarget:self action:@selector(demoButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.userInfoLabel = [[UILabel alloc] init];
    self.userInfoLabel.font = [UIFont systemFontOfSize:18];
    self.userInfoLabel.numberOfLines = 0;
    self.userInfoLabel.frame = CGRectMake(CGRectGetMinX(self.loginButton.frame), CGRectGetMaxY(self.loginButton.frame) + 50.0, CGRectGetWidth(self.urlTextField.frame), 150.0);
    [self.view addSubview:self.userInfoLabel];
    
    self.navigationController.navigationBar.translucent = YES;
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)loginButtonAction{
    NSLog(@"点击登录");
    LZLoginViewController *loginVC = [[LZLoginViewController alloc] init];
    loginVC.delegate = self;
    UINavigationController *loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
    [self.navigationController presentViewController:loginNav animated:YES completion:nil];
}

#pragma mark - LoginViewDelegate
- (void)loginWithUserInfo:(NSString *)userInfo{
    self.userInfoLabel.text = userInfo;
}

- (void)demoButtonAction{
    //  跳转到web 页面
    ThorWebViewController *marsWebVC = [[ThorWebViewController alloc] init];
    marsWebVC.delegate = self;
    [self.navigationController pushViewController:marsWebVC animated:YES];
}

/** 返回第三方APP首页 */
- (void)thorReturnHomePage{
    NSLog(@"返回首页");
}
/** 退出MarsWebViewController */
- (void)thorWebViewExit{
    NSLog(@"退出");
}

/** 认证结果 resultCode=1 成功， resultCode=0 失败 */
- (void)thorRiskResultWithVC:(ThorWebViewController *)thorVC andCode:(NSString *)resultCode resultParam:(NSDictionary *)resultData{
    // 三方根据code做相应的跳转处理
    [thorVC.navigationController popViewControllerAnimated:YES];
}

/** 支付结果 resultCode=1 支付成功， resultCode=0 支付失败 */
- (void)thorPayResultWithVC:(ThorWebViewController *)thorVC andCode:(NSString *)resultCode resultParam:(NSDictionary *)resultData{
    // 三方根据code做相应的跳转处理
    [thorVC.navigationController popViewControllerAnimated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
